////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.more;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MonitorGroup;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.activity.groups.monitor.MonitorNewsDjActivity;
import cc.telecomdigital.tdstock.activity.groups.monitor.MonitorNewsFActivity;

/**
 * @author haibo
 */
public class FinetListItemContentActivity extends HttpSuperClassStockBaseActivity
{
	protected static final String	TAG	= FinetListItemContentActivity.class.getSimpleName();
	
	public static Class<? extends Activity>	_fromActivityClass	= null;
	
	public static String _iId;
	public static String _iVendor;
	
	public static String	_btnCaption;

	private WebView			_webView;
	private TextView		_newsTitle;
	private TextView		_newsTimestamp;
	private Button			_toolBtnReturn;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_news_list_item_content);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "NewContent Backforward to : " + _fromActivityClass.getSimpleName());
				
				if(_fromActivityClass == null)
					return;
				
				Intent intent = new Intent(getApplicationContext(), _fromActivityClass);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				
				if(MonitorNewsDjActivity.class.getSimpleName().equals(_fromActivityClass.getSimpleName()))
					SwitchTabHostActivity(1, MonitorGroup.GetActivityGroupHostControl(), MonitorNewsDjActivity.class ,intent);
				else if(MonitorNewsFActivity.class.getSimpleName().equals(_fromActivityClass.getSimpleName()))
					SwitchTabHostActivity(1, MonitorGroup.GetActivityGroupHostControl(), MonitorNewsFActivity.class ,intent);
				else if(FinetListItemsActivity.class.getSimpleName().equals(_fromActivityClass.getSimpleName()))
				{
					FinetListItemsActivity.isBackCurrentPage = true;
					SwitchForwardActivity(_fromActivityClass, intent);
				}
			}
		});

		_newsTimestamp = (TextView) findViewById(R.id.list_item_timestamp);
		_newsTitle = (TextView) findViewById(R.id.list_item_title);

		_webView = (WebView) findViewById(R.id.webview);
		WebSettings webSettings = _webView.getSettings();
		webSettings.setDefaultFontSize(24);
	}

	@Override
	protected void onResume()
	{
		if(_webView == null)
			_webView = (WebView) findViewById(R.id.webview);
		
		_webView.clearCache(true);
		_webView.clearView();
		_webView.clearHistory();
		
		if (appHost.screenIsLocked && iList.size() > 0)
		{
			updateData(iList);
		}
		
		super.onResume();
		
		_toolBtnReturn.setText(_btnCaption);
	}
	
	protected void onPause()
	{
		super.onPause();
		_webView.clearSslPreferences();
		_webView.loadUrl("about:blank");
		_webView.clearHistory();
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		// share the same activity with monitor tab
		return MoreGroup.GetActivityGroupHostControl() == null ? MonitorGroup.GetActivityGroupHostControl() : MoreGroup.GetActivityGroupHostControl();
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		_newsTitle.setText("");
		_newsTimestamp.setText("");
		
		if(_iVendor == null || _iId == null)
			return null;
		return HttpAppInfo.SendDjFNewsContentRequest(actContext, _iVendor, _iId, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(final H_UserClient_HttpRequest_Result iResult)
	{
		if (!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError + "(" + iResult.MStatus + ")");
			return;
		}
		
		Runnable runnable = new Runnable()
		{
			@Override
			public void run()
			{
				updateData(iResult.contentStores);
			}
		};
		_messageHandler.post(runnable);
	}
	
	private List<String[]> iList = new ArrayList<String[]>();
	
	private void updateData(List<String[]> list)
	{
		String errorMsg = list.get(0)[1];
		String[] contentStr = list.get(1);
		
		StringBuffer _newsContent = new StringBuffer();
		String _timestamp;
		String _title;
		
		if (errorMsg != null && !"".equals(errorMsg.trim()))
		{
			_newsContent.setLength(0);
			_newsContent.append(errorMsg);
			_timestamp = "";
			_title = "";
		}
		else
		{
			String datetime = contentStr[0];
			_timestamp = datetime.length() >= 16 ? datetime.substring(0, 16) : datetime;
			_title = contentStr[1];
			_newsContent.append(contentStr[2]);
		}
		
		if (_title != null)
		{
			_newsTitle.setText(Html.fromHtml(_title));
		}
		_newsTitle.setVisibility((_title != null && _title.length() >= 1) ? View.VISIBLE : View.INVISIBLE);

		if (_timestamp != null)
		{
			_newsTimestamp.setText(_timestamp);
		}

		if (_webView != null)
		{
			String summary = "<html><body><div>" + _newsContent.toString().replaceAll("[\n]", "<br>").replaceAll("&gt;", ">").replaceAll("&lt;", "<").replaceAll("&amp;", "&") + "</div></body></html>";
			
			TDStockLog.d(TAG, summary);
			
			_webView.getSettings().setJavaScriptEnabled(true);
			_webView.getSettings().setBuiltInZoomControls(true);
			// _webView.loadData(summary, "text/html", "utf-8");
			_webView.loadDataWithBaseURL(null, summary, "text/html", "utf-8", null);
//			_webView.loadDataWithBaseURL(null, _newsContent.toString(), "text/html", "utf-8", null);
		}
		
		iList.clear();
		iList.addAll(list);
	}
}
